package com.spring.security.config.security.authentication;

import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
 * @Author wsh
 * @Date 2020/7/16 16:16
 * @Desc 校验验证码
 */
public class CustomWebAuthenticationDetails extends WebAuthenticationDetails {
    private boolean imageCodeIsRight;

    public boolean getImageCodeIsRight(){
        return this.imageCodeIsRight;
    }

    public CustomWebAuthenticationDetails(HttpServletRequest request) {
        super(request);
        String captcha = request.getParameter("captcha");
        HttpSession session = request.getSession();
        String savedImageCode = (String) session.getAttribute("captcha");
        if (!StringUtils.isEmpty(savedImageCode)){
            //清楚验证码，不论是否登录成功
            session.removeAttribute("captcha");
            if (!StringUtils.isEmpty(captcha) && captcha.equals(savedImageCode)){
                this.imageCodeIsRight = true;
            }
        }

    }
}
